package com.xiaomi.shwx.controller;

import com.xiaomi.shwx.common.enums.WorkOrderEvents;
import com.xiaomi.shwx.common.enums.WorkOrderStatus;
import com.xiaomi.shwx.common.result.Result;
import com.xiaomi.shwx.common.statemachine.WorkOrderProcessor;
import com.xiaomi.shwx.entity.pojo.WorkOrder;
import com.xiaomi.shwx.mapper.WorkOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/order")
public class TestController {
    @Autowired
    private WorkOrderMapper workOrderMapper;

    @Autowired
    private WorkOrderProcessor workOrderProcessor;

    @PostMapping("/create")
    public Result create(@RequestBody WorkOrder workOrder) {
        workOrder.setStatus("0");
        workOrderMapper.insert(workOrder);
        return Result.ok();
    }

    @PostMapping("/accept")
    public Result accept(@RequestParam("id") Long id) {
        WorkOrder workOrder = workOrderMapper.selectById(id);
        workOrder.setWorkOrderStatus(WorkOrderStatus.getByStatus(workOrder.getStatus()));
        workOrderProcessor.process(workOrder, WorkOrderEvents.WORKER_ACCEPT_ORDER);
        return Result.ok();
    }

    @PostMapping("/confirm")
    public Result confirm(@RequestParam("id") Long id) {
        WorkOrder workOrder = workOrderMapper.selectById(id);
        workOrder.setWorkOrderStatus(WorkOrderStatus.getByStatus(workOrder.getStatus()));
        workOrderProcessor.process(workOrder, WorkOrderEvents.USER_CONFIRM_ORDER);
        return Result.ok();
    }

    @PostMapping("/check")
    public Result check(@RequestParam("id") Long id) {
        WorkOrder workOrder = workOrderMapper.selectById(id);
        workOrder.setWorkOrderStatus(WorkOrderStatus.getByStatus(workOrder.getStatus()));
        workOrderProcessor.process(workOrder, WorkOrderEvents.WORKER_INSPECTION);
        return Result.ok();
    }

    @PostMapping("/cancel")
    public Result cancel(@RequestParam("id") Long id) {
        WorkOrder workOrder = workOrderMapper.selectById(id);
        workOrder.setWorkOrderStatus(WorkOrderStatus.getByStatus(workOrder.getStatus()));
        workOrderProcessor.process(workOrder, WorkOrderEvents.USER_CANCEL_ORDER);
        return Result.ok();
    }

    @PostMapping("/canceltest")
    public Result push_test(@RequestParam("id") Long id) {
        WorkOrder workOrder = workOrderMapper.selectById(id);
        workOrder.setWorkOrderStatus(WorkOrderStatus.getByStatus(workOrder.getStatus()));
        workOrderProcessor.process(workOrder, WorkOrderEvents.USER_CANCEL_ORDER);
        return Result.ok();
    }



}
